﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinalCountdownServices.Models;
using Microsoft.Phone.Scheduler;

namespace FinalCountdownServices
{

    public interface IReminderService
    {
        void AddOrModifyReminder(EventModel model);
        void RemoveReminder(EventModel model);
    }

    public class ReminderService : IReminderService
    {
        private const string ReminderPreFix = "FinalCountdownReminder_";
        public void AddOrModifyReminder(EventModel model)
        {
            var reminderKey = ReminderPreFix + model.Id;
            
            var objReminder = ScheduledActionService.Find(reminderKey) as Reminder;
            if (objReminder != null)
            {
                ScheduledActionService.Remove(reminderKey);
            }

            objReminder = new Reminder(reminderKey);
            var beginTime = model.Occurs.AddMinutes(model.NotificationMinutes * -1);
            objReminder.BeginTime = beginTime;
            objReminder.Title = model.Title;
            objReminder.Content = model.Description;           
            objReminder.NavigationUri = new Uri("/Views/EventPage.xaml?EventId=" + model.Id, UriKind.Relative);
            if (beginTime > DateTime.Now)
                ScheduledActionService.Add(objReminder);

        }
        public void RemoveReminder(EventModel model)
        {
            var reminderKey = ReminderPreFix + model.Id;
            
            var objReminder = ScheduledActionService.Find(reminderKey) as Reminder;
            if (objReminder != null)
            {
                ScheduledActionService.Remove(reminderKey);
            }
        }

    }
}
